package com.mca.framework.completableFuture;

import java.util.concurrent.*;

/**
 * @Author: zhoupeng
 * @createTime: 2025年07月07日 22:12:19
 * @version: 1.0
 * @Description:
 * @copyright:
 */
public class RunAfterEitherDemo {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService executorService = Executors.newFixedThreadPool(100);

        CompletableFuture future1 = CompletableFuture.supplyAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("future1："+Thread.currentThread().getName());
            return "hello";
        },executorService);


        CompletableFuture<String> future2 = CompletableFuture.supplyAsync(()-> {
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("future2："+Thread.currentThread().getName());
            return "itheima";
        },executorService);
       //future1 与  future2 任一先执行完  ,但是还是 future2 的返回结果

        future1.runAfterEitherAsync(future2, ()-> {
            try {
                System.out.println("其中一个任务处理完成了"+future2.get());
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        },executorService);
        System.out.println(future1.get());
    }
}
